//  合并两个数组
function mergeSort(left, right) {
  let i = (j = 0);
  let res = [];
  while (i < left.length && j < right.length) {
    if (left[i] < right[j]) {
      res.push(left[i++]);
    } else {
      res.push(right[j++]);
    }
  }
  return res.concat(left.slice(i)).concat(right.slice(j));
}
// 拆分数组成最小单元
function merge(arr) {
  let length = arr.length;
  if (length <= 1) {
    return arr;
  }
  let mid = Math.floor(length / 2);
  let left = arr.slice(0, mid);
  let right = arr.slice(mid);
  return mergeSort(merge(left), merge(right));
}

let arr = [1, 3, 4, 2, 5, 9, 7, -1];
let res = merge(arr);
console.log(res);
